package com.example.demo;

import com.example.demo.async.AsyncTask;
import com.example.demo.restTemplate.RestUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@RestController
@RequestMapping("/app")
public class TestController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private AsyncTask asyncTask;

    @Autowired
    private RestUtil restUtil;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private List<Miao> list=new ArrayList<Miao>();

    @PostMapping("/test/uo")
    public String get(@RequestBody TokenJ tokenJ) {
        System.out.println("tokenJ:"+tokenJ);
        return tokenJ.toString();
    }

    @RequestMapping("task")
    public String enity() {
        String url = "https://free-api.heweather.com/v5/forecast?city=CN101080101&key=5c043b56de9f4371b0c7f8bee8f5b75e";
        Map map = new HashMap();
        map.put("page", 1);
        String result = restUtil.getEnity(url, null);
        Map<Thread, StackTraceElement[]> maps = Thread.getAllStackTraces();
        for (Thread t : maps.keySet()) {
            System.out.println("thredname:" + t.getName());
        }
        return result;
    }


    @RequestMapping("setlist")
    public String setList() {
        ListOperations<String,String> ops=stringRedisTemplate.opsForList();
        //生产数据放入消息队列
      for(int i=0;i<100;i++) {
          ops.rightPush("listl","商品"+i);
      }
        return "SUCCESS";
    }


    @RequestMapping("map")
    public String map() {

        HashOperations<String,String,String> has=stringRedisTemplate.opsForHash();
        //stringRedisTemplate.expire()
        System.out.println(has.entries("map"));
        has.put("map1","1","1");
        has.put("map1","2","2");
        has.put("map1","3","2");
        has.put("map1","4","2");
        has.put("map1","5","2");
        System.out.println(has.get("map1","3"));
        System.out.println(has.get("map1","1"));
        System.out.println(has.get("map1","2"));
        System.out.println(has.get("map1","5"));
        System.out.println(has.entries("map1"));
        stringRedisTemplate.delete("map1");
        System.out.println("rand:"+stringRedisTemplate.randomKey());
        System.out.println(has.entries("map1"));
        return "SUCCESS";
    }


    @RequestMapping("getList")
    public String getList() {
        //stringRedisTemplate.opsForList().remove()参数意义
       ListOperations<String,String> ops=stringRedisTemplate.opsForList();
                    //  stringRedisTemplate.opsForSet().pop()是不是也是线程安全，拿出并删除？
        //vo.remove("bag", 3, "||");？？？


//        //判断能否购买商品
//        if(1==1){
//
//        }
//
//        String str = ops.rightPop("lis");
//        if(str!=null){
//            //秒杀成功
//
//        }
//        System.out.println("str="+str);
//        System.out.println("list="+ops.range("lis",0,-1));
//        List<String> list=ops.range("lis",0,-1);
//        System.out.println("list="+list);

        ExecutorService er= Executors.newFixedThreadPool(10);
        for(int i=0;i<10000;i++){
            Miao miao=new Miao();
            miao.setId(i);
            miao.setUserId(i);
            er.submit(new RunableEnable(ops,miao,list,stringRedisTemplate));
        }
        return null;
    }

    @GetMapping("getValue")
    public void getValue(){
        int t= new Random().nextInt();
        System.out.println(stringRedisTemplate.opsForValue().append("trte","uuuuu"));

        System.out.println(stringRedisTemplate.opsForList().leftPush("d","1"));

        System.out.println(stringRedisTemplate.opsForList().leftPush("d","1"));

        System.out.println(stringRedisTemplate.opsForList().leftPush("d","1"));

        System.out.println(stringRedisTemplate.opsForList().leftPush("d","1"));
        //stringRedisTemplate.opsForList().remove("d",2,"d");
        //stringRedisTemplate.opsForList().trim("d",0,2);
        //stringRedisTemplate.opsForSet().difference(),set的 API,???
        System.out.println(stringRedisTemplate.opsForList().range("d",0,-1));

    }
}
